package heima.lm.oschina.adapter;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import heima.lm.oschina.BaseApplication;
import heima.lm.oschina.R;
import heima.lm.oschina.bean.Blog;
import heima.lm.oschina.constant.Constants;
import heima.lm.oschina.utils.SpUtil;
import heima.lm.oschina.utils.UIUtils;

/**
 * Created by 金毛兄弟 on 2017/4/5.
 */

public class BlogRvAdapter extends BaseRecyclerViewAdapter {


    public BlogRvAdapter(List list) {
        super(list);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(BaseApplication.mApp).inflate(R.layout.item_rv_blog, parent, false);
        return new BlogViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        super.onBindViewHolder(holder, position);
        BlogViewHolder viewHolder = (BlogViewHolder) holder;
        final Blog blog = (Blog) mList.get(position);
        viewHolder.mTvBlogDocumentType.setVisibility(View.VISIBLE);
        if (blog.getDocumenttype() == Blog.DOC_TYPE_ORIGINAL) {
            viewHolder.mTvBlogDocumentType.setImageResource(R.drawable.widget_original_icon);
        } else {
            viewHolder.mTvBlogDocumentType.setImageResource(R.drawable.widget_repaste_icon);
        }
        String ids = SpUtil.getString(Constants.NEWS_IDS, "");
        if (ids.contains(String.valueOf(blog.getId()))) {
            viewHolder.mTvBlogTitle.setTextColor(UIUtils.getColor(R.color.gray));
            viewHolder.mTvBlogBody.setTextColor(UIUtils.getColor(R.color.gray));
        } else {
            viewHolder.mTvBlogTitle.setTextColor(UIUtils.getColor(R.color.text_dark));
            viewHolder.mTvBlogBody.setTextColor(UIUtils.getColor(R.color.text_dark));
        }
        viewHolder.mTvBlogTitle.setText(blog.getTitle());
        viewHolder.mTvBlogBody.setText(blog.getBody());
        viewHolder.mTvBlogBody.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                copyToClipBoard(blog.getBody());
                return false;
            }
        });
        viewHolder.mTvBlogAuthor.setText(blog.getAuthor());
        viewHolder.mTvBlogDate.setText(blog.getPubDate());
        viewHolder.mTvBlogCommentCount.setText(String.valueOf(blog.getCommentCount()));

    }

    private void copyToClipBoard(CharSequence text) {
        ClipboardManager cm = (ClipboardManager) BaseApplication.mApp.getSystemService(Context.CLIPBOARD_SERVICE);
        cm.setText(text);
        Toast.makeText(BaseApplication.mApp, "已复制到剪贴板", Toast.LENGTH_SHORT).show();
    }

    class BlogViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_blog_documenttype)
        ImageView mTvBlogDocumentType;
        @BindView(R.id.tv_blog_title)
        TextView mTvBlogTitle;
        @BindView(R.id.tv_blog_body)
        TextView mTvBlogBody;
        @BindView(R.id.tv_blog_author)
        TextView mTvBlogAuthor;
        @BindView(R.id.tv_blog_date)
        TextView mTvBlogDate;
        @BindView(R.id.tv_blog_comment_count)
        TextView mTvBlogCommentCount;


        public BlogViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);

        }
    }
}
